﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace APP.HELPER.Utils.OSInfo
{
    public class OSInfo
    {
        private struct OSVERSIONINFOEX
        {
            public int dwOSVersionInfoSize;
            public int dwMajorVersion;
            public int dwMinorVersion;
            public int dwBuildNumber;
            public int dwPlatformId;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string szCSDVersion;
            public short wServicePackMajor;
            public short wServicePackMinor;
            public short wSuiteMask;
            public byte wProductType;
            public byte wReserved;
        }
        public enum EnumOS
        {
            Unknown,
            Windows_31,
            Windows_95,
            Windows_98_Second_Edition,
            Windows_98,
            Windows_Me,
            Windows_NT_3_51,
            Windows_NT_40,
            Windows_2000,
            Windows_XP,
            Windows_Media_Center,
            Windows_Server_2003,
            Windows_Server_2008,
            Windows_Longhorn,
            Windows_Vista,
            Windows_7,
            Windows_8
        }
        private const int VER_NT_WORKSTATION = 1;
        private const int VER_NT_DOMAIN_CONTROLLER = 2;
        private const int VER_NT_SERVER = 3;
        private const int VER_SUITE_SMALLBUSINESS = 1;
        private const int VER_SUITE_ENTERPRISE = 2;
        private const int VER_SUITE_TERMINAL = 16;
        private const int VER_SUITE_DATACENTER = 128;
        private const int VER_SUITE_SINGLEUSERTS = 256;
        private const int VER_SUITE_PERSONAL = 512;
        private const int VER_SUITE_BLADE = 1024;
        public static string OSVersion
        {
            get
            {
                return Environment.OSVersion.Version.ToString();
            }
        }
        public static int OSMajorVersion
        {
            get
            {
                return Environment.OSVersion.Version.Major;
            }
        }
        public static int OSMinorVersion
        {
            get
            {
                return Environment.OSVersion.Version.Minor;
            }
        }
        public static int OSBuildVersion
        {
            get
            {
                return Environment.OSVersion.Version.Build;
            }
        }
        public static int OSRevisionVersion
        {
            get
            {
                return Environment.OSVersion.Version.Revision;
            }
        }
        [DllImport("kernel32.dll")]
        private static extern bool GetVersionEx(ref OSInfo.OSVERSIONINFOEX osVersionInfo);
        public static string GetOSProductType()
        {
            OSInfo.OSVERSIONINFOEX oSVERSIONINFOEX = default(OSInfo.OSVERSIONINFOEX);
            OperatingSystem oSVersion = Environment.OSVersion;
            oSVERSIONINFOEX.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSInfo.OSVERSIONINFOEX));
            string result;
            if (!OSInfo.GetVersionEx(ref oSVERSIONINFOEX))
            {
                result = "";
            }
            else
            {
                if (oSVersion.Version.Major == 4)
                {
                    if (oSVERSIONINFOEX.wProductType == 1)
                    {
                        result = " Workstation";
                    }
                    else
                    {
                        if (oSVERSIONINFOEX.wProductType == 3)
                        {
                            result = " Server";
                        }
                        else
                        {
                            result = "";
                        }
                    }
                }
                else
                {
                    if (oSVersion.Version.Major == 5)
                    {
                        if (oSVERSIONINFOEX.wProductType == 1)
                        {
                            if ((oSVERSIONINFOEX.wSuiteMask & 512) == 512)
                            {
                                result = " Home Edition";
                                return result;
                            }
                            result = " Professional";
                            return result;
                        }
                        else
                        {
                            if (oSVERSIONINFOEX.wProductType == 3)
                            {
                                if (oSVersion.Version.Minor == 0)
                                {
                                    if ((oSVERSIONINFOEX.wSuiteMask & 128) == 128)
                                    {
                                        result = " Datacenter Server";
                                        return result;
                                    }
                                    if ((oSVERSIONINFOEX.wSuiteMask & 2) == 2)
                                    {
                                        result = " Advanced Server";
                                        return result;
                                    }
                                    result = " Server";
                                    return result;
                                }
                                else
                                {
                                    if ((oSVERSIONINFOEX.wSuiteMask & 128) == 128)
                                    {
                                        result = " Datacenter Edition";
                                        return result;
                                    }
                                    if ((oSVERSIONINFOEX.wSuiteMask & 2) == 2)
                                    {
                                        result = " Enterprise Edition";
                                        return result;
                                    }
                                    if ((oSVERSIONINFOEX.wSuiteMask & 1024) == 1024)
                                    {
                                        result = " Web Edition";
                                        return result;
                                    }
                                    result = " Standard Edition";
                                    return result;
                                }
                            }
                        }
                    }
                    result = "";
                }
            }
            return result;
        }
        public static string GetOSServicePack()
        {
            OSInfo.OSVERSIONINFOEX oSVERSIONINFOEX = default(OSInfo.OSVERSIONINFOEX);
            oSVERSIONINFOEX.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSInfo.OSVERSIONINFOEX));
            string result;
            if (!OSInfo.GetVersionEx(ref oSVERSIONINFOEX))
            {
                result = "";
            }
            else
            {
                result = " " + oSVERSIONINFOEX.szCSDVersion;
            }
            return result;
        }
        public static string GetOSName()
        {
            OperatingSystem oSVersion = Environment.OSVersion;
            string result = "UNKNOWN";
            switch (oSVersion.Platform)
            {
                case PlatformID.Win32Windows:
                    {
                        int minor = oSVersion.Version.Minor;
                        if (minor != 0)
                        {
                            if (minor != 10)
                            {
                                if (minor == 90)
                                {
                                    result = "Windows Me";
                                }
                            }
                            else
                            {
                                if (oSVersion.Version.Revision.ToString() == "2222A")
                                {
                                    result = "Windows 98 Second Edition";
                                }
                                else
                                {
                                    result = "Windows 98";
                                }
                            }
                        }
                        else
                        {
                            result = "Windows 95";
                        }
                        break;
                    }
                case PlatformID.Win32NT:
                    switch (oSVersion.Version.Major)
                    {
                        case 3:
                            result = "Windows NT 3.51";
                            break;
                        case 4:
                            result = "Windows NT 4.0";
                            break;
                        case 5:
                            if (oSVersion.Version.Minor == 0)
                            {
                                result = "Windows 2000";
                            }
                            else
                            {
                                if (oSVersion.Version.Minor == 1)
                                {
                                    result = "Windows XP";
                                }
                                else
                                {
                                    if (oSVersion.Version.Minor == 2)
                                    {
                                        result = "Windows Server 2003";
                                    }
                                }
                            }
                            break;
                        case 6:
                            if (oSVersion.Version.Major == 0)
                            {
                                result = "Windows Vista";
                            }
                            else
                            {
                                if (oSVersion.Version.Major == 1)
                                {
                                    result = "Windows 7";
                                }
                            }
                            break;
                    }
                    break;
            }
            return result;
        }
        public static OSInfo.EnumOS GetOS()
        {
            OperatingSystem oSVersion = Environment.OSVersion;
            OSInfo.EnumOS result = OSInfo.EnumOS.Unknown;
            switch (oSVersion.Platform)
            {
                case PlatformID.Win32Windows:
                    {
                        int minor = oSVersion.Version.Minor;
                        if (minor != 0)
                        {
                            if (minor != 10)
                            {
                                if (minor == 90)
                                {
                                    result = OSInfo.EnumOS.Windows_Me;
                                }
                            }
                            else
                            {
                                if (oSVersion.Version.Revision.ToString() == "2222A")
                                {
                                    result = OSInfo.EnumOS.Windows_98_Second_Edition;
                                }
                                else
                                {
                                    result = OSInfo.EnumOS.Windows_98;
                                }
                            }
                        }
                        else
                        {
                            result = OSInfo.EnumOS.Windows_95;
                        }
                        break;
                    }
                case PlatformID.Win32NT:
                    switch (oSVersion.Version.Major)
                    {
                        case 3:
                            result = OSInfo.EnumOS.Windows_NT_3_51;
                            break;
                        case 4:
                            result = OSInfo.EnumOS.Windows_NT_40;
                            break;
                        case 5:
                            if (oSVersion.Version.Minor == 0)
                            {
                                result = OSInfo.EnumOS.Windows_2000;
                            }
                            else
                            {
                                if (oSVersion.Version.Minor == 1)
                                {
                                    result = OSInfo.EnumOS.Windows_XP;
                                }
                                else
                                {
                                    if (oSVersion.Version.Minor == 2)
                                    {
                                        result = OSInfo.EnumOS.Windows_Server_2003;
                                    }
                                }
                            }
                            break;
                        case 6:
                            if (oSVersion.Version.Minor == 0)
                            {
                                result = OSInfo.EnumOS.Windows_Vista;
                            }
                            else
                            {
                                if (oSVersion.Version.Minor == 1)
                                {
                                    result = OSInfo.EnumOS.Windows_7;
                                }
                            }
                            break;
                    }
                    break;
            }
            return result;
        }
    }
}
